package github.kawaiior.yuanchuxiuzhen.common.item.fire;

import github.kawaiior.yuanchuxiuzhen.common.block.fire.StrangeFire;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.itemgroup.YcxzGroup;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;

public class StrangeFireItem extends Item {
    private final float mana;
    private final StrangeFire fire;
    public StrangeFireItem(float mana, StrangeFire fire) {
        super(new Properties().group(YcxzGroup.YCXZ_GROUP));
        this.mana = mana;
        this.fire = fire;
    }

    public int getLevel(){
        return this.fire.getLevel();
    }

    @Override
    public ActionResultType onItemUse(ItemUseContext context) {
        World world = context.getWorld();
        if (!world.isRemote) {
            PlayerEntity player = context.getPlayer();

            if (player.isCreative()){
                Vector3d vector3d = context.getHitVec();
                world.setBlockState(new BlockPos(vector3d), fire.getDefaultState());
                return ActionResultType.func_233537_a_(world.isRemote);
            }

            LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
            capability.ifPresent((power)->{
                if (power.consumeMana(player, world, mana)){
                    ItemStack itemstack = context.getItem();
                    Vector3d vector3d = context.getHitVec();
                    world.setBlockState(new BlockPos(vector3d), fire.getDefaultState());
                    itemstack.shrink(1);
                }else {
                    // TODO: send player mana is not enough
                }
            });
        }

        return ActionResultType.func_233537_a_(world.isRemote);
    }

    public static IItemProvider getYangStrangeFire(int level){
        switch (level) {
            case 1:
                return ItemInit.YANG_FIRE_1.get();
            case 2:
                return ItemInit.YANG_FIRE_2.get();
            case 3:
                return ItemInit.YANG_FIRE_3.get();
            case 4:
                return ItemInit.YANG_FIRE_4.get();
            case 5:
                return ItemInit.YANG_FIRE_5.get();
            case 6:
                return ItemInit.YANG_FIRE_6.get();
            case 7:
                return ItemInit.YANG_FIRE_7.get();
            case 8:
                return ItemInit.YANG_FIRE_8.get();
            case 9:
                return ItemInit.YANG_FIRE_9.get();
        }
        return ItemInit.YANG_FIRE_1.get();
    }

    public static IItemProvider getYinStrangeFire(int level){
        switch (level) {
            case 1:
                return ItemInit.YIN_FIRE_1.get();
            case 2:
                return ItemInit.YIN_FIRE_2.get();
            case 3:
                return ItemInit.YIN_FIRE_3.get();
            case 4:
                return ItemInit.YIN_FIRE_4.get();
            case 5:
                return ItemInit.YIN_FIRE_5.get();
            case 6:
                return ItemInit.YIN_FIRE_6.get();
            case 7:
                return ItemInit.YIN_FIRE_7.get();
            case 8:
                return ItemInit.YIN_FIRE_8.get();
            case 9:
                return ItemInit.YIN_FIRE_9.get();
        }
        return ItemInit.YIN_FIRE_1.get();
    }

}
